package cc.rengu.redp.bizimpl.upmp.controller;


import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.domain.RedpResponse;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.bizimpl.upmp.domain.WctFstInfo;
import cc.rengu.redp.bizimpl.upmp.domain.WctSecInfo;
import cc.rengu.redp.bizimpl.upmp.service.WctFstInfoService;
import cc.rengu.redp.bizimpl.upmp.service.WctSecInfoService;
import cc.rengu.redp.common.utils.SortUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * @author LiuYuLiang.LL
 */
@Slf4j
@Validated
@Api(tags = "参数管理:微信二级类目管理")
@RestController
@RequestMapping("/admin/merParam/wctSecInfo")
public class WctSecInfoController extends BaseController {
    @Autowired
    private WctSecInfoService wctSecInfoService;
    @Autowired
    private WctFstInfoService wctFstInfoService;

    @GetMapping
//    @RequiresPermissions("wechatSecond:list")
    public Map<String, Object> list(QueryRequest request, WctSecInfo entity) {
        return getDataTable(this.wctSecInfoService.findByPage(request, entity));
    }

    @Log("查询单条")
    @GetMapping(value = "/getOne")
//    @RequiresPermissions("wechatSecond:view")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public WctSecInfo getOne(@NotBlank(message = "{required}") String id) {
        return wctSecInfoService.getById(id);
    }

    @Log("新增")
    @PostMapping
//    @RequiresPermissions("wechatSecond:add")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "wctSecNo", value = "微信商户二级类目代码",required = true,  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "wctSecName", value = "微信商户二级类目描述", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "wctUpNo", value = "所属上级类目代码", required = true, dataType = "String")
    })
    public void add(@Valid WctSecInfo entity) throws RedpException {
        try {
            this.wctSecInfoService.create(entity);
        } catch (Exception e) {
            String message = "新增失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("修改")
    @PutMapping("/update")
//    @RequiresPermissions("wechatSecond:update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    })
    public void update(@Valid WctSecInfo entity) throws RedpException {
        try {
            this.wctSecInfoService.update(entity);
        } catch (Exception e) {
            String message = "修改失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("注销")
    @PutMapping("/cancel")
//    @RequiresPermissions("wechatSecond:cancel")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public void cancel(@NotBlank(message = "{required}") String id) throws RedpException {
        try {
            this.wctSecInfoService.cancel(id);
        } catch (Exception e) {
            String message = "注销失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("删除")
    @DeleteMapping("/{ids}")
//    @RequiresPermissions("wechatSecond:delete")
    @ApiImplicitParam(paramType = "path", name = "ids", value = "id主键，逗号隔开", required = true, dataType = "String")
    public void delete(@NotBlank(message = "{required}") @PathVariable String ids) throws RedpException {
        try {
            this.wctSecInfoService.delete(ids.split(StringPool.COMMA));
        } catch (Exception e) {
            String message = "删除失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @PostMapping(value = "/check")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id主键",  dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "wctSecNo", value = "二级类目代码",  dataType = "String")
           ,@ApiImplicitParam(paramType = "query", name = "wctSecName", value = "二级类目描述",  dataType = "String")
    })
    public RedpResponse check(String id, String wctSecNo, String wctSecName) throws RedpException {
        LambdaQueryWrapper<WctSecInfo> wrapper = new LambdaQueryWrapper();
        if (StringUtils.checkValNotNull(wctSecNo)) {
            if (StringUtils.checkValNotNull(id)) {
                wrapper.ne(WctSecInfo::getId, id);
            }
            wrapper.eq(WctSecInfo::getWctSecNo, wctSecNo);
            WctSecInfo info = wctSecInfoService.getOne(wrapper);
            if (null != info) {
                throw new RedpException("[ 二级类目代码 ] 重复，请重新输入");
            }
        }
        if (StringUtils.checkValNotNull(wctSecName)) {
            wrapper = new LambdaQueryWrapper();
            if (StringUtils.checkValNotNull(id)) {
                wrapper.ne(WctSecInfo::getId, id);
            }
            wrapper.likeRight(WctSecInfo::getWctSecName, wctSecName);
            WctSecInfo info = wctSecInfoService.getOne(wrapper);
            if (null != info) {
                throw new RedpException("[ 二级类目描述 ] 重复，请重新输入");
            }
        }
        return new RedpResponse().message("验证成功");
    }


    /**
     * 查询上级类目代码 --makeby lizk
     *
     * @param q
     * @param request
     * @return
     */
    @GetMapping("wctfstSelect2List")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "q", value = "参数", required = true, dataType = "String")
    })
    public Map<String, Object> agentmanageorgSelect2List(String q, QueryRequest request){
        return wctFstInfoService.wctfstSelect2List(q, request);
    }
}
